<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\exception\DbException;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class Photos extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function mylist(Request $request)
    {
        $data = $request->param();
        self::checktoken($data['id']);
        $page  = isset($data['page']) ? $data['page'] : 1;
        $listrow  = isset($data['listrow']) ? $data['listrow'] : 1;
        $list = \db('home_photos')->where('uid',$data['id'])
            ->field('id picid,uid,pics,create_time')
            ->paginate($listrow,true,['page'=>$page]);
        return apistatus(1,'获取成功',$list);
    }

    /**
     * 添加评论
     *
     * @return \think\Response
     */
    public function create(Request $request)
    {
        $data = $request->param();
        self::checktoken($data['id']);
        $insert = [
            'pics' => $data['pics'],
            'uid' => $data['id'],
            'create_time' => time(),
        ];
        $res = \db('home_photos')->insert($insert);
        if ($res){
            return apistatus(1,'保存成功');
        }else{
            return apistatus(0,'保存失败');
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(Request $request)
    {
        $data = $request->param();
        $page  = isset($data['page']) ? $data['page'] : 1;
        $listrow  = isset($data['listrow']) ? $data['listrow'] : 1;
        $list = \db('home_photos')->where('uid',$data['toid'])
            ->paginate($listrow,true,['page'=>$page]);
        return apistatus(1,'获取成功',$list);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        //
        $data = $request->param();
        self::checktoken($data['id']);
        $where = [
            'id'=>$data['picid'],
            'uid'=>$data['id'],
        ];
        $res = \db('home_photos')->where($where)->delete();
        if ($res){
            return apistatus(1,'删除成功');
        }else{
            return apistatus(0,'失败，请重试');
        }
    }
}
